//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import android.os.SystemClock;

public class SensorInfo {
	
	private int 	sMaxDataLength;
	private float 	sMaxVal;	
	
	public float [] sDataVal = null;		// Sensor data value
	public long [] sDataTime = null;		// Sensor data time stamp
	
	private int sDataCurrent;
	private float sCurrent;
	private float sMax;
	private float sMin;
	private float sAverage;
	private float sAverageN;
	
	public int getDataCurrent() { return sDataCurrent; }
	public float getCurrent() { return sCurrent; }
	public float getMax() { return sMax; }
	public float getMin() { return sMin; }
	
	public void setMaxMin(float Max, float Min) {
		sMax = Max;
		sMin = Min;
	}
	
	public float getBufferFraction() {
		return (float)sDataCurrent/(float)sMaxDataLength;
	}
	
	public SensorInfo(int pMaxDataLength, float pMaxVal) {
		
		sMaxDataLength = pMaxDataLength;
		sMaxVal = pMaxVal;
		
		sDataVal = new float[sMaxDataLength];
		sDataTime = new long[sMaxDataLength];
		
		reset();
	}
	
	public void reset() {
		sDataCurrent = 0;
		sCurrent = 0.0f;
		sMax = -sMaxVal;
		sMin =  sMaxVal;
		
		sAverage = 0.0f;
		sAverageN = 0.0f;
		
		// Reset data to non-display value
		synchronized (this) {
        	for (int i=0;i<sMaxDataLength;i++) {	        		
        		sDataVal[i] = sMaxVal;
        		sDataTime[i] = 0;
        	} 
		}
	}
	
	public void set(float val) {
		
		// Set current value with time stamp
		sCurrent = val;
		if (sCurrent > sMaxVal) sCurrent =  sMaxVal; // Positive safety bound
		if (sCurrent <-sMaxVal) sCurrent = -sMaxVal; // Negative safety bound
		if (sCurrent > sMax) sMax = sCurrent;
		if (sCurrent < sMin) sMin = sCurrent;
		synchronized (this) {
			sDataVal[sDataCurrent] = sCurrent;
			sDataTime[sDataCurrent] = SystemClock.elapsedRealtime();
		}
		sDataCurrent = (sDataCurrent + 1)%sMaxDataLength;
	}
	
	public void setAverage(float val) {
		
		// For fast-changing data sets like sensors, take the average of 10 samples
		sAverage += val;
		sAverageN += 1.0f;
		if (sAverageN > 10.0f) {
			sCurrent = sAverage/sAverageN;
			sAverage = 0.0f;
			sAverageN = 0.0f;
			if (sCurrent > sMaxVal) sCurrent =  sMaxVal; // Positive safety bound
			if (sCurrent <-sMaxVal) sCurrent = -sMaxVal; // Negative safety bound
			if (sCurrent > sMax) sMax = sCurrent;
			if (sCurrent < sMin) sMin = sCurrent;
			synchronized (this) {
				sDataVal[sDataCurrent] = sCurrent;
				sDataTime[sDataCurrent] = SystemClock.elapsedRealtime();
			}
			sDataCurrent = (sDataCurrent + 1)%sMaxDataLength;
		}
	}
}
